package cn.cqie.attendance.manager.service.impl;

import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.manager.dao.AdminClassesDao;
import cn.cqie.attendance.manager.dao.AdminStudentDao;
import cn.cqie.attendance.manager.pojo.AdminClasses;
import cn.cqie.attendance.manager.pojo.AdminStudent;
import cn.cqie.attendance.manager.pojo.AdminStudentSerch;
import cn.cqie.attendance.manager.service.AdminStudentService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 * @author 697
 * @date 2021/6/18 16:55
 */
@Service
public class AdminStudentServiceImpl implements AdminStudentService {

    @Autowired
    private AdminStudentDao adminStudentDao;

    @Autowired
    private AdminClassesDao adminClassesDao;



    /**
     * 根据班级ID查询该班级下所有学生信息
     * @param classesId 班级ID
     * @return
     */
    @Override
    public PageBean<AdminStudent> queryAllStudentByClassesId(Integer classesId, Integer pageSize, Integer pageNum) {

        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);

        List<AdminStudent> list = adminStudentDao.queryAllStudentByClassesId(classesId);
        int total = list.size();

        if (null == list || total == 0) {
            return new PageBean<>(201,"error",list);
        }
        return new PageBean<>(200,"success",list);
    }


    /**
     * 查询某个班级下的学生信息，可模糊查询 组合查询
     * @param adminStudentSerch
     * @param pageSize 每一页数量
     * @param pageNum 页数
     * @return
     */
    @Override
    public PageBean<AdminStudent> queryAllStudentByClassesIdByCombination(AdminStudentSerch adminStudentSerch, Integer pageSize, Integer pageNum) {


        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
     PageHelper.startPage(pageNum,pageSize);

        List<AdminStudent> list = adminStudentDao.queryAllStudentByClassesIdByCombination(adminStudentSerch);
        int total = list.size();

        if (null == list || total == 0) {
            return new PageBean<>(201,"error",list);
        }
        return new PageBean<>(200,"success",list);
    }


    /**
     * 给某个班级新增学生
     * @param id ID
     * @param stuNumber 学号
     * @param stuName 学生姓名
     * @param identity 身份证号
     * @param gender 性别 0女 1男
     * @param email 邮箱
     * @param phone 手机号
     * @param direction 项目角色
     * @return
     */
    @Override
    public Page addStudentToClasses(Integer id,String stuNumber, String stuName, String identity, String gender, String email, String phone, Integer direction) {
        List<AdminClasses> list = adminClassesDao.queryClassesInfoById(id);
        System.out.println("测试list:"+list);
        AdminStudent student = new AdminStudent();
        //获取班级信息并赋值
        if (list.size()!=0){
                AdminClasses s = list.get(0);
                //测试输出班级信息
                System.out.println("测试输出stuClass："+s.getId());
                System.out.println("测试输出stuMajor："+s.getMajorId());
                System.out.println("测试输出stuGrade："+s.getGradeId());
                //赋值班级信息
                student.setStuClass(s.getId());
                student.setStuMajor(s.getMajorId());
                student.setStuGrade(s.getGradeId());
        }
        //学生信息赋值
        student.setStuName(stuName);
        student.setStuNumber(stuNumber);
        student.setGender(gender);
        student.setIdentity(identity);
        student.setEmail(email);
        student.setPhone(phone);
        student.setDirection(direction);

        student.setSubmitTime(new Date());
        //判断是否重复
        int i = adminStudentDao.queryStudentByClassIDAndStuNumber(student);
        System.out.println(i + "数量");
        if (i>0){
            return Page.INFOERROR;
        }
        //继续执行新增
        else {
            int msg = adminStudentDao.addStudentToClasses(student);
            if (msg > 0) {
                return Page.SUCCESS;
            } else {
                return Page.FAIL;
            }
        }


    }


    /**
     * 根据班级ID查询班级信息
     * @param id
     * @return
     */
    @Override
    public Page<AdminStudent> queryClassesInfoById(Integer id) {
        List<AdminStudent> list = adminStudentDao.queryClassesInfoById(id);
        int total = list.size();
        if (null == list ||  total== 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success",0,0, total, list);
    }


    /**
     * 查询某个班级是否存在某个学生
     * @param adminStudent 班级ID、学生姓名、id
     * @param
     * @return
     */
    @Override
    public int queryStudentByClassIDAndStuNumber(AdminStudent adminStudent) {
        int i = adminStudentDao.queryStudentByClassIDAndStuNumber(adminStudent);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }


    /**
     * 修改某个班级学生信息
     * @param classId 班级ID
     * @param stuId 学生ID
     * @param stuNumber 学号
     * @param stuName 学生姓名
     * @param identity 身份证
     * @param gender 性别 0女 1男
     * @param email 邮箱
     * @param phone 手机号
     * @param direction 方向
     * @return
     */
    @Override
    public Page updateStudentById(Integer classId, Integer stuId, String stuNumber, String stuName, String identity, String gender, String email, String phone, Integer direction) {
        List<AdminClasses> list = adminClassesDao.queryClassesInfoById(classId);
        System.out.println("测试list:"+list);
        AdminStudent student = new AdminStudent();
        //获取班级信息并赋值
        if (list.size()!=0){
            AdminClasses s = list.get(0);
            //测试输出班级信息
            System.out.println("测试输出stuClass："+s.getId());
            System.out.println("测试输出stuMajor："+s.getMajorId());
            System.out.println("测试输出stuGrade："+s.getGradeId());
            //赋值班级信息
            student.setStuClass(s.getId());
            student.setStuMajor(s.getMajorId());
            student.setStuGrade(s.getGradeId());
        }
        //学生信息赋值
        student.setId(stuId);
        student.setStuName(stuName);
        student.setStuNumber(stuNumber);
        student.setGender(gender);
        student.setIdentity(identity);
        student.setEmail(email);
        student.setPhone(phone);
        student.setDirection(direction);
        student.setSubmitTime(new Date());
        //判断是否重复
        int i = adminStudentDao.queryStudentByClassIDAndStuNumber(student);
        if (i>0){
            return Page.INFOERROR;
        }
        //继续执行修改
        else {
            int msg = adminStudentDao.updateStudentById(student,stuId,classId);
            if (msg > 0) {
                return Page.SUCCESS;
            } else {
                return Page.FAIL;
            }
        }
    }


    /**
     * 通过学生id批量删除(某个班级的)学生、支持批量删除(单独删除)
     * @param list
     * @return
     */
    @Override
    public Page deleteStudentById(List list) {
        int i1 = 0;
        for (int i = 0;i<list.size();i++ ){
            int studentId = (Integer) list.get(i);
            i1 = adminStudentDao.deleteStudentById(studentId);
            i1++;
        }
        if (i1>0){
            return Page.SUCCESS;
        }else{
            return Page.INFOERROR;
        }
    }


    /**
     * 设置组长
     * @param studentId 学生id
     * @param groupId 小组id
     * @return
     */
    @Override
    public Page updateGroupLeaderByGroupId(Integer studentId, Integer groupId) {
        int i = adminStudentDao.updateGroupLeaderByGroupId(studentId, groupId);
       if (i>0){
           return Page.SUCCESS;
       }else{
           return Page.INFOERROR;
       }
    }


}
